<?php

//
// +---------------------------------------------------------+
// | 开发组
// +---------------------------------------------------------+
// | Copyrigh  (c)  2020 The PHP Group
// +---------------------------------------------------------+
// | 广告
// +---------------------------------------------------------+
// | zxc
// | php后端开发
// |
// +———————————————————+
//

namespace App\Models\Admin;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class AccountMoneyChange extends Model
{

    protected $table = 'account_money_change';
    protected $primaryKey = 'id';
    protected $dateFormat = 'U'; // 时间戳
    protected $casts = [
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s'
    ];

    /**
     * 充值数据
     * 添加数据
     */
    public static function addRechargeInfo($store_id, $money, $type = 0, $money_after = 0, $to_money_after = 0)
    {
        $info['account_id'] = 1;
        $info['to_account_id'] = $store_id;
        $info['money'] = $money;
        $info['consume_type'] = $type;
        $info['money_after'] = $money_after;
        $info['to_money_after'] = $to_money_after;
        $info['created_at'] = time();
        return self::insert($info);
    }

    public static function addGoodsInfo($store_id, $money, $type)
    {
    }

    /**
     * 获取资金交易记录
     * @param array $param
     * @param int $limit
     * @return mixed
     */
    public static function getList($param = [], $limit = 15)
    {
        if (!empty($param['start']) && !empty($param['end']) && count($param['account_ids']) == 0) {
            $sql = '(select * from e_account_money_change where created_at >= ' . strtotime($param['start']) . ' and created_at <= ' . strtotime($param['end']) . ' order by id desc ) as cc';
        } elseif (empty($param['start']) && empty($param['end']) && count($param['account_ids']) > 0) {
            $str = implode(',', $param['account_ids']);
            $sql = "(select * from e_account_money_change where account_id in (" . $str . ") or to_account_id in (" . $str . ") order by id desc ) as cc";
        } elseif (!empty($param['start']) && !empty($param['end']) && count($param['account_ids']) > 0) {
            $str = implode(',', $param['account_ids']);
            $sql = '(select * from e_account_money_change where (created_at >= ' . strtotime($param['start']) . ' and created_at <= ' . strtotime($param['end']) . ' and account_id in (' . $str . ') ) or (created_at >= ' . strtotime($param['start']) . ' and created_at <= ' . strtotime($param['end']) . ' and to_account_id in (' . $str . ') ) order by id desc ) as cc';
        } else {
            $sql = '(select * from e_account_money_change order by id desc) as cc';
        }
        $result = DB::table(DB::raw($sql))->paginate($limit);



        /*// 即可查看执行的sql，传入的参数等等
        $startTime = $param['start'] ? strtotime($param['start']) : 0;
        $endTime   = $param['end'] ? strtotime($param['end']) : time();
        $db = self::from('account_money_change as c')
              ->select(['c.*'])
              ->leftJoin('account as a1', 'a1.id', '=', 'c.account_id')
              ->leftJoin('account as a2', 'a2.id', '=', 'c.to_account_id')
              ->whereBetween('c.created_at', [$startTime, $endTime]);
        if ($param['store_id']) {
            $db->where([['a1.account_id', '=', $param['store_id'], 'or'], ['a2.account_id', '=', $param['store_id'], 'or']]);
        }
        $result = $db->paginate($limit);*/

        return $result;
    }
}